<?php

namespace Common\Model;

/**
 * 组信息数组
 * @modify trainyao 2016-07-26 创建
 * @index isdelete_adminLoginId(`isdelete`, `adminlogin_id`)
 */
class GroupInfoModel extends BaseModel {

    protected $trueTableName = 'tb_group_info';
    protected $field = 'id,name,adminlogin_id,isdelete';


    /**
     * 根据超级管理员获取组信息
     * @modify trainyao 2016-07-26 创建
     * @param  integer   $userId    超级管理员ID
     * @return array              组信息数组
     */
    public function getInfoByUser($userId) {
        $option = array(
            'isdelete' => NOT_DELETE,
            'adminlogin_id' => $userId,
        );
        $data = $this->where($option)->field($this->field)->select();

        return empty($data) ? array() : $data;
    }

    /**
     * 增加多个组
     * @modify trainyao 2016-07-26  创建
     * @param  array   $groupInfo 组信息 {id, name, creator, operator}
     */
    public function addInfo($groupInfo) {
        return $this->addAll($groupInfo);
    }

    /**
     * 保存组信息
     * @modify trainyao 2016-07-26 创建
     * @param  integer   $groupId   组ID
     * @param  string   $name      组名
     * @return boolean              修改执行结果
     */
    public function saveInfo($groupId, $name) {
        $option = array(
            'id' => $groupId,
        );
        $data = array(
            'name' => $name,
        );

        return $this->where($option)->save($data) === false ? false : true;
    }

    /**
     * 删除多个组
     * @modify trainyao 2016-07-26 创建
     * @param  mixin   $groupId 组ID(s)
     * @return boolean                执行结果
     */
    public function deleteInfo($groupId) {
        if (!is_array($groupId)) {
            $groupId = explode(',', $groupId);
        }

        $option = array(
            'id' => count($groupId) == 1 ? $groupId[0] : array('in', $groupId),
        );
        $data = array(
            'isdelete' => IS_DELETE,
        );

        return $this->where($option)->save($data) === false ? false : true;
    }
}
